<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>xhr_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_labs_net_xhr_test.js.html">xhr_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.labs.net.xhrTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.labs.net.xhrTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.Promise&#39;);
<a name="line19"></a>goog.require(&#39;goog.labs.net.xhr&#39;);
<a name="line20"></a>goog.require(&#39;goog.net.XmlHttp&#39;);
<a name="line21"></a>goog.require(&#39;goog.string&#39;);
<a name="line22"></a>goog.require(&#39;goog.testing.AsyncTestCase&#39;);
<a name="line23"></a>goog.require(&#39;goog.testing.MockClock&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line25"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line26"></a>
<a name="line27"></a>function setupStubXMLHttpRequest() {
<a name="line28"></a>
<a name="line29"></a>  mockClock = new goog.testing.MockClock(true);
<a name="line30"></a>
<a name="line31"></a>  var stubXhr = {
<a name="line32"></a>    sent: false,
<a name="line33"></a>    aborted: false,
<a name="line34"></a>    status: 0,
<a name="line35"></a>    headers: {},
<a name="line36"></a>    open: function(method, url, async) {
<a name="line37"></a>      this.method = method;
<a name="line38"></a>      this.url = url;
<a name="line39"></a>      this.async = async;
<a name="line40"></a>    },
<a name="line41"></a>    setRequestHeader: function(key, value) {
<a name="line42"></a>      this.headers[key] = value;
<a name="line43"></a>    },
<a name="line44"></a>    overrideMimeType: function(mimeType) {
<a name="line45"></a>      this.mimeType = mimeType;
<a name="line46"></a>    },
<a name="line47"></a>    abort: function() {
<a name="line48"></a>      this.aborted = true;
<a name="line49"></a>      this.load(0);
<a name="line50"></a>    },
<a name="line51"></a>    send: function(data) {
<a name="line52"></a>      this.data = data;
<a name="line53"></a>      this.sent = true;
<a name="line54"></a>    },
<a name="line55"></a>    load: function(status) {
<a name="line56"></a>      this.status = status;
<a name="line57"></a>      this.readyState = 4;
<a name="line58"></a>      if (this.onreadystatechange) this.onreadystatechange();
<a name="line59"></a>    }
<a name="line60"></a>  };
<a name="line61"></a>
<a name="line62"></a>  goog.net.XmlHttp = function() {
<a name="line63"></a>    return stubXhr;
<a name="line64"></a>  };
<a name="line65"></a>  for (var x in originalXmlHttp) {
<a name="line66"></a>    goog.net.XmlHttp[x] = originalXmlHttp[x];
<a name="line67"></a>  }
<a name="line68"></a>
<a name="line69"></a>  return stubXhr;
<a name="line70"></a>}
<a name="line71"></a>
<a name="line72"></a>
<a name="line73"></a>var xhr = goog.labs.net.xhr;
<a name="line74"></a>var originalXmlHttp = goog.net.XmlHttp;
<a name="line75"></a>var mockClock;
<a name="line76"></a>
<a name="line77"></a>var testCase = new goog.testing.AsyncTestCase(document.title);
<a name="line78"></a>testCase.stepTimeout = 5 * 1000;
<a name="line79"></a>
<a name="line80"></a>testCase.autoDiscoverTests();
<a name="line81"></a>
<a name="line82"></a>testCase.tearDown = function() {
<a name="line83"></a>  if (mockClock) {
<a name="line84"></a>    mockClock.dispose();
<a name="line85"></a>    mockClock = null;
<a name="line86"></a>  }
<a name="line87"></a>  goog.net.XmlHttp = originalXmlHttp;
<a name="line88"></a>};
<a name="line89"></a>
<a name="line90"></a>G_testRunner.initialize(testCase);
<a name="line91"></a>
<a name="line92"></a>// Many tests don&#39;t work on the local file system due to cross-origin
<a name="line93"></a>// restrictions in Chrome without --allow-file-access-from-files.
<a name="line94"></a>// They will run on the farm or on a Closure Test server.
<a name="line95"></a>var shouldRunLocally = goog.userAgent.IE || goog.userAgent.GECKO ||
<a name="line96"></a>    goog.string.startsWith(document.location.href, &#39;file://&#39;);
<a name="line97"></a>
<a name="line98"></a>function testSimpleRequest() {
<a name="line99"></a>  if (shouldRunLocally) return;
<a name="line100"></a>
<a name="line101"></a>  testCase.waitForAsync(&#39;simpleRequest&#39;);
<a name="line102"></a>  xhr.send(&#39;GET&#39;, &#39;testdata/xhr_test_text.data&#39;).then(function(xhr) {
<a name="line103"></a>    assertEquals(&#39;Just some data.&#39;, xhr.responseText);
<a name="line104"></a>    assertEquals(200, xhr.status);
<a name="line105"></a>    testCase.continueTesting();
<a name="line106"></a>  }, fail /* opt_onRejected */);
<a name="line107"></a>}
<a name="line108"></a>
<a name="line109"></a>function testGetText() {
<a name="line110"></a>  if (shouldRunLocally) return;
<a name="line111"></a>
<a name="line112"></a>  testCase.waitForAsync(&#39;getText&#39;);
<a name="line113"></a>  xhr.get(&#39;testdata/xhr_test_text.data&#39;).then(function(responseText) {
<a name="line114"></a>    assertEquals(&#39;Just some data.&#39;, responseText);
<a name="line115"></a>    testCase.continueTesting();
<a name="line116"></a>  }, fail /* opt_onRejected */);
<a name="line117"></a>}
<a name="line118"></a>
<a name="line119"></a>function testGetTextWithJson() {
<a name="line120"></a>  if (shouldRunLocally) return;
<a name="line121"></a>
<a name="line122"></a>  testCase.waitForAsync(&#39;getTextWithJson&#39;);
<a name="line123"></a>  xhr.get(&#39;testdata/xhr_test_json.data&#39;).then(function(responseText) {
<a name="line124"></a>    assertEquals(&#39;while(1);\n{&quot;stat&quot;:&quot;ok&quot;,&quot;count&quot;:12345}\n&#39;, responseText);
<a name="line125"></a>    testCase.continueTesting();
<a name="line126"></a>  }, fail /* opt_onRejected */);
<a name="line127"></a>}
<a name="line128"></a>
<a name="line129"></a>function testPostText() {
<a name="line130"></a>  if (shouldRunLocally) return;
<a name="line131"></a>
<a name="line132"></a>  testCase.waitForAsync(&#39;postText&#39;);
<a name="line133"></a>  xhr.post(&#39;testdata/xhr_test_text.data&#39;, &#39;post-data&#39;).then(
<a name="line134"></a>      function(responseText) {
<a name="line135"></a>        // No good way to test post-data gets transported.
<a name="line136"></a>        assertEquals(&#39;Just some data.&#39;, responseText);
<a name="line137"></a>        testCase.continueTesting();
<a name="line138"></a>      }, fail /* opt_onRejected */);
<a name="line139"></a>}
<a name="line140"></a>
<a name="line141"></a>function testGetJson() {
<a name="line142"></a>  if (shouldRunLocally) return;
<a name="line143"></a>
<a name="line144"></a>  testCase.waitForAsync(&#39;getJson&#39;);
<a name="line145"></a>  xhr.getJson(&#39;testdata/xhr_test_json.data&#39;, {xssiPrefix: &#39;while(1);\n&#39;}).then(
<a name="line146"></a>      function(responseObj) {
<a name="line147"></a>        assertEquals(&#39;ok&#39;, responseObj[&#39;stat&#39;]);
<a name="line148"></a>        assertEquals(12345, responseObj[&#39;count&#39;]);
<a name="line149"></a>        testCase.continueTesting();
<a name="line150"></a>      }, fail /* opt_onRejected */);
<a name="line151"></a>}
<a name="line152"></a>
<a name="line153"></a>function testSerialRequests() {
<a name="line154"></a>  if (shouldRunLocally) return;
<a name="line155"></a>
<a name="line156"></a>  xhr.get(&#39;testdata/xhr_test_text.data&#39;).
<a name="line157"></a>      then(function(response) {
<a name="line158"></a>        return xhr.getJson(
<a name="line159"></a>            &#39;testdata/xhr_test_json.data&#39;, {xssiPrefix: &#39;while(1);\n&#39;});
<a name="line160"></a>      }).then(function(responseObj) {
<a name="line161"></a>        // Data that comes through to callbacks should be from the 2nd request.
<a name="line162"></a>        assertEquals(&#39;ok&#39;, responseObj[&#39;stat&#39;]);
<a name="line163"></a>        assertEquals(12345, responseObj[&#39;count&#39;]);
<a name="line164"></a>        testCase.continueTesting();
<a name="line165"></a>      }, fail /* opt_onRejected */);
<a name="line166"></a>}
<a name="line167"></a>
<a name="line168"></a>function testBadUrlDetectedAsError() {
<a name="line169"></a>  if (shouldRunLocally) return;
<a name="line170"></a>
<a name="line171"></a>  testCase.waitForAsync(&#39;badUrl&#39;);
<a name="line172"></a>  xhr.getJson(&#39;unknown-file.dat&#39;).then(
<a name="line173"></a>      fail /* opt_onFulfilled */,
<a name="line174"></a>      function(err) {
<a name="line175"></a>        assertTrue(
<a name="line176"></a>            &#39;Error should be an HTTP error&#39;, err instanceof xhr.HttpError);
<a name="line177"></a>        assertEquals(404, err.status);
<a name="line178"></a>        assertNotNull(err.xhr);
<a name="line179"></a>        testCase.continueTesting();
<a name="line180"></a>      });
<a name="line181"></a>}
<a name="line182"></a>
<a name="line183"></a>function testBadOriginTriggersOnErrorHandler() {
<a name="line184"></a>  testCase.waitForAsync(&#39;badOrigin&#39;);
<a name="line185"></a>  xhr.get(&#39;http://www.google.com&#39;).then(
<a name="line186"></a>      fail /* opt_onFulfilled */,
<a name="line187"></a>      function(err) {
<a name="line188"></a>        // In IE this will be a goog.labs.net.xhr.Error since it is thrown
<a name="line189"></a>        //  when calling xhr.open(), other browsers will raise an HttpError.
<a name="line190"></a>        assertTrue(&#39;Error should be an xhr error&#39;, err instanceof xhr.Error);
<a name="line191"></a>        assertNotNull(err.xhr);
<a name="line192"></a>        testCase.continueTesting();
<a name="line193"></a>      });
<a name="line194"></a>}
<a name="line195"></a>
<a name="line196"></a>function testAbortRequest() {
<a name="line197"></a>  if (shouldRunLocally) return;
<a name="line198"></a>
<a name="line199"></a>  testCase.waitForAsync(&#39;abortRequest&#39;);
<a name="line200"></a>  var promise = xhr.send(&#39;GET&#39;, &#39;test-url&#39;, null).then(
<a name="line201"></a>      fail /* opt_onFulfilled */,
<a name="line202"></a>      function(error, result) {
<a name="line203"></a>        assertTrue(error instanceof goog.Promise.CancellationError);
<a name="line204"></a>        testCase.continueTesting();
<a name="line205"></a>      });
<a name="line206"></a>  promise.cancel();
<a name="line207"></a>}
<a name="line208"></a>
<a name="line209"></a>//============================================================================
<a name="line210"></a>// The following tests are synchronous and use a stubbed out XMLHttpRequest.
<a name="line211"></a>//============================================================================
<a name="line212"></a>
<a name="line213"></a>function testSendNoOptions() {
<a name="line214"></a>  var stubXhr = setupStubXMLHttpRequest();
<a name="line215"></a>  var called = false;
<a name="line216"></a>  xhr.send(&#39;GET&#39;, &#39;test-url&#39;, null).then(function(xhr) {
<a name="line217"></a>    called = true;
<a name="line218"></a>    assertEquals(&#39;Objects should be equal&#39;, xhr, stubXhr);
<a name="line219"></a>  }, fail /* opt_onRejected */);
<a name="line220"></a>
<a name="line221"></a>  assertTrue(&#39;XHR should have been sent&#39;, stubXhr.sent);
<a name="line222"></a>  assertFalse(&#39;Callback should not yet have been called&#39;, called);
<a name="line223"></a>
<a name="line224"></a>  stubXhr.load(200);
<a name="line225"></a>  mockClock.tick();
<a name="line226"></a>
<a name="line227"></a>  assertTrue(&#39;Callback should have been called&#39;, called);
<a name="line228"></a>  assertEquals(&#39;GET&#39;, stubXhr.method);
<a name="line229"></a>  assertEquals(&#39;test-url&#39;, stubXhr.url);
<a name="line230"></a>}
<a name="line231"></a>
<a name="line232"></a>function testSendPostSetsDefaultHeader() {
<a name="line233"></a>  var stubXhr = setupStubXMLHttpRequest();
<a name="line234"></a>  xhr.send(&#39;POST&#39;, &#39;test-url&#39;, null).
<a name="line235"></a>      then(undefined /* opt_onResolved */, fail /* opt_onRejected */);
<a name="line236"></a>
<a name="line237"></a>  stubXhr.load(200);
<a name="line238"></a>  mockClock.tick();
<a name="line239"></a>
<a name="line240"></a>  assertEquals(&#39;POST&#39;, stubXhr.method);
<a name="line241"></a>  assertEquals(&#39;test-url&#39;, stubXhr.url);
<a name="line242"></a>  assertEquals(&#39;application/x-www-form-urlencoded;charset=utf-8&#39;,
<a name="line243"></a>      stubXhr.headers[&#39;Content-Type&#39;]);
<a name="line244"></a>}
<a name="line245"></a>
<a name="line246"></a>function testSendPostHeaders() {
<a name="line247"></a>  var stubXhr = setupStubXMLHttpRequest();
<a name="line248"></a>  xhr.send(&#39;POST&#39;, &#39;test-url&#39;, null, {
<a name="line249"></a>    headers: {&#39;Content-Type&#39;: &#39;text/plain&#39;, &#39;X-Made-Up&#39;: &#39;FooBar&#39;}
<a name="line250"></a>  }).then(undefined /* opt_onResolved */, fail /* opt_onRejected */);
<a name="line251"></a>
<a name="line252"></a>  stubXhr.load(200);
<a name="line253"></a>  mockClock.tick();
<a name="line254"></a>
<a name="line255"></a>  assertEquals(&#39;POST&#39;, stubXhr.method);
<a name="line256"></a>  assertEquals(&#39;test-url&#39;, stubXhr.url);
<a name="line257"></a>  assertEquals(&#39;text/plain&#39;, stubXhr.headers[&#39;Content-Type&#39;]);
<a name="line258"></a>  assertEquals(&#39;FooBar&#39;, stubXhr.headers[&#39;X-Made-Up&#39;]);
<a name="line259"></a>}
<a name="line260"></a>
<a name="line261"></a>function testSendWithCredentials() {
<a name="line262"></a>  var stubXhr = setupStubXMLHttpRequest();
<a name="line263"></a>  xhr.send(&#39;POST&#39;, &#39;test-url&#39;, null, {withCredentials: true}).
<a name="line264"></a>      then(undefined /* opt_onResolved */, fail /* opt_onRejected */);
<a name="line265"></a>
<a name="line266"></a>  stubXhr.load(200);
<a name="line267"></a>  mockClock.tick();
<a name="line268"></a>
<a name="line269"></a>  assertTrue(&#39;XHR should have been sent&#39;, stubXhr.sent);
<a name="line270"></a>  assertTrue(stubXhr.withCredentials);
<a name="line271"></a>}
<a name="line272"></a>
<a name="line273"></a>function testSendWithMimeType() {
<a name="line274"></a>  var stubXhr = setupStubXMLHttpRequest();
<a name="line275"></a>  xhr.send(&#39;POST&#39;, &#39;test-url&#39;, null, {mimeType: &#39;text/plain&#39;}).
<a name="line276"></a>      then(undefined /* opt_onResolved */, fail /* opt_onRejected */);
<a name="line277"></a>
<a name="line278"></a>  stubXhr.load(200);
<a name="line279"></a>  mockClock.tick();
<a name="line280"></a>
<a name="line281"></a>  assertTrue(&#39;XHR should have been sent&#39;, stubXhr.sent);
<a name="line282"></a>  assertEquals(&#39;text/plain&#39;, stubXhr.mimeType);
<a name="line283"></a>}
<a name="line284"></a>
<a name="line285"></a>function testSendWithHttpError() {
<a name="line286"></a>  var stubXhr = setupStubXMLHttpRequest();
<a name="line287"></a>  var err;
<a name="line288"></a>  xhr.send(&#39;POST&#39;, &#39;test-url&#39;, null).then(
<a name="line289"></a>      fail /* opt_onResolved */,
<a name="line290"></a>      function(error) { err = error; } /* opt_onRejected */);
<a name="line291"></a>
<a name="line292"></a>  stubXhr.load(500);
<a name="line293"></a>  mockClock.tick();
<a name="line294"></a>
<a name="line295"></a>  assertTrue(&#39;XHR should have been sent&#39;, stubXhr.sent);
<a name="line296"></a>  assertTrue(err instanceof xhr.HttpError);
<a name="line297"></a>  assertEquals(500, err.status);
<a name="line298"></a>}
<a name="line299"></a>
<a name="line300"></a>function testSendWithTimeoutNotHit() {
<a name="line301"></a>  // TODO(user): This test fails in safari if it is run as part of a batch
<a name="line302"></a>  // but passes when run on its own.  Something strange is going on to do
<a name="line303"></a>  // with the references to window.clearTimeout inside onreadystatechange and
<a name="line304"></a>  // the mockclock overrides.
<a name="line305"></a>  if (goog.userAgent.SAFARI) return;
<a name="line306"></a>
<a name="line307"></a>  var stubXhr = setupStubXMLHttpRequest();
<a name="line308"></a>  var err;
<a name="line309"></a>  xhr.send(&#39;POST&#39;, &#39;test-url&#39;, null, {timeoutMs: 1500}).
<a name="line310"></a>      then(undefined /* opt_onResolved */, fail /* opt_onRejected */);
<a name="line311"></a>  assertTrue(mockClock.getTimeoutsMade() &gt; 0);
<a name="line312"></a>  mockClock.tick(1400);
<a name="line313"></a>  stubXhr.load(200);
<a name="line314"></a>  mockClock.tick(200);
<a name="line315"></a>  assertTrue(&#39;XHR should have been sent&#39;, stubXhr.sent);
<a name="line316"></a>  assertFalse(&#39;XHR should not have been aborted&#39;, stubXhr.aborted);
<a name="line317"></a>}
<a name="line318"></a>
<a name="line319"></a>function testSendWithTimeoutHit() {
<a name="line320"></a>  var stubXhr = setupStubXMLHttpRequest();
<a name="line321"></a>  var err;
<a name="line322"></a>  xhr.send(&#39;POST&#39;, &#39;test-url&#39;, null, {timeoutMs: 50}).then(
<a name="line323"></a>      fail /* opt_onResolved */,
<a name="line324"></a>      function(error) { err = error; } /* opt_onRejected */);
<a name="line325"></a>  assertTrue(mockClock.getTimeoutsMade() &gt; 0);
<a name="line326"></a>  mockClock.tick(50);
<a name="line327"></a>  assertTrue(&#39;XHR should have been sent&#39;, stubXhr.sent);
<a name="line328"></a>  assertTrue(&#39;XHR should have been aborted&#39;, stubXhr.aborted);
<a name="line329"></a>  assertTrue(err instanceof xhr.TimeoutError);
<a name="line330"></a>}
<a name="line331"></a>
<a name="line332"></a>function testCancelRequest() {
<a name="line333"></a>  var stubXhr = setupStubXMLHttpRequest();
<a name="line334"></a>  var err;
<a name="line335"></a>  var promise = xhr.send(&#39;GET&#39;, &#39;test-url&#39;, null, {timeoutMs: 50}).then(
<a name="line336"></a>      fail /* opt_onResolved */,
<a name="line337"></a>      function(error) {
<a name="line338"></a>        err = error;
<a name="line339"></a>        if (error instanceof goog.Promise.CancellationError) {
<a name="line340"></a>          stubXhr.abort();
<a name="line341"></a>        }
<a name="line342"></a>      });
<a name="line343"></a>  promise.cancel();
<a name="line344"></a>  stubXhr.load(0);  // Call load anyway, shoudn&#39;t make a difference.
<a name="line345"></a>  mockClock.tick(100);  // Timeout should never be called.
<a name="line346"></a>
<a name="line347"></a>  assertTrue(&#39;XHR should have been sent&#39;, stubXhr.sent);
<a name="line348"></a>  assertTrue(&#39;XHR should have been aborted&#39;, stubXhr.aborted);
<a name="line349"></a>  assertTrue(err instanceof goog.Promise.CancellationError);
<a name="line350"></a>}
<a name="line351"></a>
<a name="line352"></a>function testGetJson() {
<a name="line353"></a>  var stubXhr = setupStubXMLHttpRequest();
<a name="line354"></a>  var responseData;
<a name="line355"></a>  xhr.getJson(&#39;test-url&#39;).then(function(responseObj) {
<a name="line356"></a>    responseData = responseObj;
<a name="line357"></a>  }, fail /* opt_onRejected */);
<a name="line358"></a>
<a name="line359"></a>  stubXhr.responseText = &#39;{&quot;a&quot;: 1, &quot;b&quot;: 2}&#39;;
<a name="line360"></a>  stubXhr.load(200);
<a name="line361"></a>  mockClock.tick();
<a name="line362"></a>
<a name="line363"></a>  assertObjectEquals({a: 1, b: 2}, responseData);
<a name="line364"></a>}
<a name="line365"></a>
<a name="line366"></a>function testGetJsonWithXssiPrefix() {
<a name="line367"></a>  var stubXhr = setupStubXMLHttpRequest();
<a name="line368"></a>  var responseData;
<a name="line369"></a>  xhr.getJson(&#39;test-url&#39;, {xssiPrefix: &#39;while(1);\n&#39;}).then(
<a name="line370"></a>      function(responseObj) { responseData = responseObj; },
<a name="line371"></a>      fail /* opt_onRejected */);
<a name="line372"></a>
<a name="line373"></a>  stubXhr.responseText = &#39;while(1);\n{&quot;a&quot;: 1, &quot;b&quot;: 2}&#39;;
<a name="line374"></a>  stubXhr.load(200);
<a name="line375"></a>  mockClock.tick();
<a name="line376"></a>
<a name="line377"></a>  assertObjectEquals({a: 1, b: 2}, responseData);
<a name="line378"></a>}
<a name="line379"></a>
<a name="line380"></a>function testSendWithClientException() {
<a name="line381"></a>  var stubXhr = setupStubXMLHttpRequest();
<a name="line382"></a>  stubXhr.send = function(data) {
<a name="line383"></a>    throw new Error(&#39;CORS XHR with file:// schemas not allowed.&#39;);
<a name="line384"></a>  };
<a name="line385"></a>  var err;
<a name="line386"></a>  xhr.send(&#39;POST&#39;, &#39;file://test-url&#39;, null).then(
<a name="line387"></a>      fail /* opt_onResolved */,
<a name="line388"></a>      function(error) { err = error; } /* opt_onRejected */);
<a name="line389"></a>
<a name="line390"></a>  stubXhr.load(0);
<a name="line391"></a>  mockClock.tick();
<a name="line392"></a>
<a name="line393"></a>  assertFalse(&#39;XHR should not have been sent&#39;, stubXhr.sent);
<a name="line394"></a>  assertTrue(err instanceof Error);
<a name="line395"></a>  assertTrue(
<a name="line396"></a>      /CORS XHR with file:\/\/ schemas not allowed./.test(err.message));
<a name="line397"></a>}
</pre>


</body>
</html>
